package cn.iocoder.yudao.module.bpm.controller.admin.tbprocess;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bpm.controller.admin.tbprocess.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.tbprocess.TbProcessDO;
import cn.iocoder.yudao.module.bpm.convert.tbprocess.TbProcessConvert;
import cn.iocoder.yudao.module.bpm.service.tbprocess.TbProcessService;

@Api(tags = "aaa管理后台 - 图斑和流程实例关联")
@RestController
@RequestMapping("/bpm/tb-process")
@Validated
public class TbProcessController {

    @Resource
    private TbProcessService tbProcessService;

    @GetMapping("/getPageTbProcess")
    @ApiOperation("多表联查获取图斑-流程分页")
    public CommonResult<Page<TbProcessManageVO>> getPageGroup(TbProcessManageVO pageVO) {
        Page<TbProcessManageVO> iPage = new Page<TbProcessManageVO>(pageVO.getPageNo(), pageVO.getPageSize());
        Page<TbProcessManageVO> groupVOPage = tbProcessService.selectPageGroup(iPage, pageVO);
        return success(groupVOPage);
    }

    @PostMapping("/create")
    @ApiOperation("创建图斑和流程实例关联")
    @PreAuthorize("@ss.hasPermission('bpm:tb-process:create')")
    public CommonResult<Long> createTbProcess(@Valid @RequestBody TbProcessCreateReqVO createReqVO) {
        return success(tbProcessService.createTbProcess(createReqVO));
    }

    @PostMapping("/createMuti")
    @ApiOperation("创建多条图斑和流程实例关联")
    public CommonResult<Long> createMutiTbProcess(@RequestBody List<TbProcessCreateReqVO> createReqVO) {
        return success(tbProcessService.createMutiTbProcess(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新图斑和流程实例关联")
    @PreAuthorize("@ss.hasPermission('bpm:tb-process:update')")
    public CommonResult<Boolean> updateTbProcess(@Valid @RequestBody TbProcessUpdateReqVO updateReqVO) {
        tbProcessService.updateTbProcess(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除图斑和流程实例关联")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('bpm:tb-process:delete')")
    public CommonResult<Boolean> deleteTbProcess(@RequestParam("id") Long id) {
        tbProcessService.deleteTbProcess(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得图斑和流程实例关联")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('bpm:tb-process:query')")
    public CommonResult<TbProcessRespVO> getTbProcess(@RequestParam("id") Long id) {
        TbProcessDO tbProcess = tbProcessService.getTbProcess(id);
        return success(TbProcessConvert.INSTANCE.convert(tbProcess));
    }

    @GetMapping("/list")
    @ApiOperation("获得图斑和流程实例关联列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('bpm:tb-process:query')")
    public CommonResult<List<TbProcessRespVO>> getTbProcessList(@RequestParam("ids") Collection<Long> ids) {
        List<TbProcessDO> list = tbProcessService.getTbProcessList(ids);
        return success(TbProcessConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得图斑和流程实例关联分页")
    @PreAuthorize("@ss.hasPermission('bpm:tb-process:query')")
    public CommonResult<PageResult<TbProcessRespVO>> getTbProcessPage(@Valid TbProcessPageReqVO pageVO) {
        PageResult<TbProcessDO> pageResult = tbProcessService.getTbProcessPage(pageVO);
        return success(TbProcessConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出图斑和流程实例关联 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:tb-process:export')")
    @OperateLog(type = EXPORT)
    public void exportTbProcessExcel(@Valid TbProcessExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TbProcessDO> list = tbProcessService.getTbProcessList(exportReqVO);
        // 导出 Excel
        List<TbProcessExcelVO> datas = TbProcessConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "图斑和流程实例关联.xls", "数据", TbProcessExcelVO.class, datas);
    }

}
